from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import chromedriver_autoinstaller
import configparser
from datetime import datetime
chromedriver_autoinstaller.install()
path= chromedriver_autoinstaller.install()
cf = configparser.ConfigParser()
cf.read("config.ini")
student_id = cf.get('user','student_id')
password = cf.get('user','password')
course_id = cf.get('user','course_id')
option = webdriver.ChromeOptions()
option.add_experimental_option("excludeSwitches", ['enable-automation','enable-logging'])
driver = webdriver.Chrome(executable_path= path, chrome_options=option)
def login():
    driver.get("https://login.sufe.edu.cn/login/")
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@id="username"]/div/div[1]/div[1]/div/input').click()
    driver.find_element(By.XPATH,'//*[@id="username"]/div/div[1]/div[1]/div/input').send_keys(student_id)
    driver.find_element(By.XPATH,'//*[@id="username"]/div/div[2]/div[1]/div/input').click()
    driver.find_element(By.XPATH,'//*[@id="username"]/div/div[2]/div[1]/div/input').send_keys(password)
    driver.find_element(By.XPATH,'//*[@id="username"]/div/button').click()
    print("登陆成功")
    time.sleep(2)
    driver.get("https://eams.sufe.edu.cn/eams/stdElectCourse!defaultPage.action?electionProfile.id=8006")
    driver.find_element(By.XPATH,' /html/body/div[10]/div[1]/div[2]').click()
    time.sleep(2)
    while True:
        driver.find_element(By.XPATH,'//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').click()
        driver.find_element(By.XPATH,'//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').send_keys(course_id)
        driver.find_element(By.XPATH,'//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').send_keys(Keys.ENTER)
        try:
            cur = driver.find_element(By.CLASS_NAME,'stdCount').text
            print(cur)
            cur = cur.split("/")
            a,b = cur[0],cur[1]
            if a<b:
                driver.find_element(By.CLASS_NAME,'lessonListOperator').click()
                print("选课成功")
                break
            else:
                print("--------"+str(datetime.now())+"--------")
                print("人数上限，抢课失败，正在刷新")
                time.sleep(1)
                driver.find_element(By.XPATH,'//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').click()
                driver.find_element(By.XPATH,'//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').clear()
                driver.find_element(By.XPATH,'//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').send_keys(Keys.ENTER)
        except:
            driver.find_element(By.XPATH, '//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').click()
            driver.find_element(By.XPATH, '//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').clear()
            driver.find_element(By.XPATH, '//*[@id="electableLessonList"]/thead/tr[1]/th[2]/div/input').send_keys(Keys.ENTER)

login()